package cn.hongwei.eduservice.controller.api;

import cn.hongwei.eduservice.entity.Chapter;
import cn.hongwei.eduservice.entity.Course;
import cn.hongwei.eduservice.entity.query.CourseApiQuery;
import cn.hongwei.eduservice.entity.vo.CourseDetailsVo;
import cn.hongwei.eduservice.service.ChapterService;
import cn.hongwei.eduservice.service.CourseService;
import cn.hongwei.result.R;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Great
 * @email 1224028994@qq.com
 */
@RestController
@CrossOrigin
@RequestMapping("/api/edu/course")
public class ApiCourseController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private ChapterService chapterService;

    //4、查询课程的章节嵌套集合
    @GetMapping("getChapterNestedList/{courseId}")
    public R getChapterNestedList(@PathVariable String courseId){
        List<Chapter> chapters = chapterService.getChaptersAndVideos(courseId);
        return R.ok().data("items" , chapters);
    }

    //1、查询指定id讲师的课程列表
    @GetMapping("getCoursesByTeacherId/{teacherId}")
    public R getCoursesByTeacherId(@PathVariable String teacherId){
        LambdaQueryWrapper<Course> lambdaQueryWrapper = new LambdaQueryWrapper();

        lambdaQueryWrapper.eq(Course::getTeacherId, teacherId);//比较课程对应的老师id
        lambdaQueryWrapper.eq(Course::getStatus, "Normal");//判断是否是已经发布的课程
        //哪些要查询的字段
        lambdaQueryWrapper.select(Course::getCover,Course::getTitle , Course::getId);
        lambdaQueryWrapper.orderByDesc(Course::getPublishTime);//根据发布时间降序排列
        List<Course> courses = courseService.list(lambdaQueryWrapper);
        return R.ok().data("items",courses);
    }

    //2、带条件查询课程列表
    @GetMapping("list")
    // url?subjectId=xxxx&subjectParentId=xxxx,通过路径封装到查询条件
    public R listCourses(CourseApiQuery courseApiQuery){
        List<Course> courses = courseService.listCourses(courseApiQuery);
        return R.ok().data("items", courses);
    }

    //3、查询课程详情
    @GetMapping("getCourseDetails/{courseId}")
    public R getCourseDetails(@PathVariable String courseId){
        CourseDetailsVo vo = courseService.getCourseDetails(courseId);
        return R.ok().data("item" , vo);
    }
}
